package services

import (
	"360/video-srv/basic/config"
	__ "360/video-srv/basic/proto"
	"360/video-srv/modler"
	"context"
	"encoding/json"
	"time"
)

func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddReq) (*__.VideoAddResp, error) {
	var video = modler.Video{
		Title:        in.Title,
		Description:  in.Description,
		CoverUrl:     in.CoverUrl,
		Category:     in.Category,
		Types:        in.Types,
		Region:       in.Region,
		Year:         int(in.Year),
		Actor:        in.Actor,
		PlayCount:    int(in.PlayCount),
		LikeCount:    int(in.LikeCount),
		CollectCount: int(in.CollectCount),
		CommentCount: int(in.CommentCount),
	}
	config.DB.Debug().Create(&video)
	return &__.VideoAddResp{
		VideoId: int64(video.ID),
	}, nil
}
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	var video []modler.Video
	var category string
	var types string
	var region string
	if in.Category == 1 {
		category = "电视剧"
	}
	if in.Category == 2 {
		category = "电影"
	}
	if in.Category == 3 {
		category = "综艺"
	}
	if in.Types == 1 {
		types = "喜剧"
	}
	if in.Types == 2 {
		types = "爱情"
	}
	if in.Types == 3 {
		types = "动作"
	}
	if in.Region == 1 {
		region = "美国"
	}
	if in.Region == 2 {
		region = "内陆"
	}
	if in.Region == 3 {
		region = "中国香港"
	}
	offset := (in.Page - 1) * in.Size

	var b []*__.VideoItem
	result, _ := config.Rdb.Get(config.Ctx, "videoList").Result()
	if result != "" {
		json.Unmarshal([]byte(result), &video)
	} else {
		config.DB.Debug().Where("category = ?", category).
			Where("types = ?", types).
			Where("region = ?", region).
			Offset(int(offset)).Limit(int(in.Size)).
			Find(&video)
		jsonData, _ := json.Marshal(&video)
		config.Rdb.Set(config.Ctx, "videoList", jsonData, 0)
	}
	for _, c := range video {
		list := __.VideoItem{
			Title:        c.Title,
			Description:  c.Description,
			CoverUrl:     c.CoverUrl,
			Category:     c.Category,
			Types:        c.Types,
			Region:       c.Region,
			Year:         int64(c.Year),
			Actor:        c.Actor,
			PlayCount:    int64(c.PlayCount),
			LikeCount:    int64(c.LikeCount),
			CollectCount: int64(c.CollectCount),
			CommentCount: int64(c.CommentCount),
		}
		b = append(b, &list)
	}
	return &__.VideoListResp{
		List: b,
	}, nil
}
func (s *Server) VideoFind(_ context.Context, in *__.VideoFindReq) (*__.VideoFindResp, error) {
	var video modler.Video
	config.DB.Debug().Where("id = ?", in.VideoId).Find(&video)

	if video.Category == "电视剧" {
		var videoEp []modler.VideoEp
		config.DB.Debug().Where("video_id = ?", in.VideoId).Find(&videoEp)
		var b []*__.VideoEpItem
		for _, c := range videoEp {
			list := __.VideoEpItem{
				VideoId: int64(c.VideoId),
				EpNum:   int64(c.EpNum),
				Title:   c.Title,
				EpTime:  c.EpTime,
			}
			b = append(b, &list)
		}
		return &__.VideoFindResp{
			List: b,
		}, nil
	}

	return &__.VideoFindResp{
		Title:        video.Title,
		Description:  video.Description,
		CoverUrl:     video.CoverUrl,
		Category:     video.Category,
		Types:        video.Types,
		Region:       video.Region,
		Year:         int64(video.Year),
		Actor:        video.Actor,
		PlayCount:    int64(video.PlayCount),
		LikeCount:    int64(video.LikeCount),
		CollectCount: int64(video.CollectCount),
		CommentCount: int64(video.CommentCount),
	}, nil
}
func (s *Server) VideoLog(_ context.Context, in *__.VideoLogReq) (*__.VideoLogResp, error) {
	var videoEp modler.VideoEp
	config.DB.Debug().Where("id = ?", in.EpId).Where("video_id = ?", in.VideoId).Find(&videoEp)
	var videoEpLog = modler.VideoEpLog{
		UserId:    int(in.UserId),
		VideoId:   int(in.VideoId),
		EpId:      int(in.EpId),
		EpTime:    videoEp.EpTime,
		EpTimeAt:  in.EpTimeAt,
		WatchTime: time.Now(),
	}
	config.DB.Debug().Where("video_id = ?", in.VideoId).Where("ep_id = ?", in.EpId).Find(&videoEpLog)
	if videoEpLog.ID == 0 {
		config.DB.Debug().Create(&videoEpLog)
	}
	config.DB.Debug().Model(&modler.VideoEpLog{}).Where("id = ?", in.EpId).Where("video_id = ?", in.VideoId).
		Update("ep_time_at", in.EpTimeAt).
		Update("watch_time", time.Now())
	return &__.VideoLogResp{VideoEpLogId: int64(videoEpLog.ID)}, nil
}
func (s *Server) VideoLogList(_ context.Context, in *__.VideoLogListReq) (*__.VideoLogListResp, error) {
	var videoEpLog []modler.VideoEpLog
	config.DB.Debug().Where("user_id = ?", in.UserId).Order("watch_time desc").Find(&videoEpLog)
	var b []*__.VideoLogItem
	for _, c := range videoEpLog {
		list := __.VideoLogItem{
			UserId:   int64(c.UserId),
			VideoId:  int64(c.VideoId),
			EpId:     int64(c.UserId),
			EpTime:   c.EpTimeAt,
			EpTimeAt: c.EpTimeAt,
		}
		b = append(b, &list)
	}
	return &__.VideoLogListResp{List: b}, nil
}
